/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugBuyPlan;
import com.jims.master.modules.drug.service.DrugBuyPlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 药品采购计划查询Controller
 * @author DT
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugPlanCheck")
public class DrugPlanCheckController extends BaseController {

	@Autowired
	private DrugBuyPlanService drugBuyPlanService;
	
	@ModelAttribute
	public DrugBuyPlan get(@RequestParam(required=false) String id) {
		DrugBuyPlan entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugBuyPlanService.get(id);
		}
		if (entity == null){
			entity = new DrugBuyPlan();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugBuyPlan drugBuyPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (null != drugBuyPlan && null != drugBuyPlan.getPurchaseNumber()) {
            drugBuyPlan.setStateFlag("4");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        } else {
            drugBuyPlan.setDelFlag(null);
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        }
		return "modules/drug/drugPlanCheckIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugBuyPlan drugBuyPlan, Model model) {
		model.addAttribute("drugBuyPlan", drugBuyPlan);
		return "modules/drug/drugPlanCheckForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugBuyPlan drugBuyPlan, Model model) {
	    StringData data=new StringData();
		int i=drugBuyPlanService.save(drugBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugBuyPlan drugBuyPlan) {
	    StringData data=new StringData();
		int i=drugBuyPlanService.delete(drugBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}